package com.gxnzd.communityelderly.commom;

import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.support.CustomSQLExceptionTranslatorRegistrar;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * ClassName: GlobalException
 * Package: com.gxnzd.communityelderly.commom
 * Description:
 *
 * @Author 胡海峰
 * @Create 2024/10/15 13:53
 * @Version 1.0
 */
@RestControllerAdvice
public class GlobalException {


    /**
     * 数据库数据重复的报错
     * @param e
     * @return
     */
    @ExceptionHandler({SQLIntegrityConstraintViolationException.class})
    public R ex(Exception e) {

        if (e.getMessage().contains("Duplicate entry")) {
            String[] s = e.getMessage().split(" ");
            String msg = s[9] + "已存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }

    /**
     * 用户token过期的报错
     * @return
     */
    @ExceptionHandler({ExpiredJwtException.class})
    public R ErrorToken(){
        return R.error("用户token已过期");
    }
}

